<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Verify the Inputs to display the content
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
// include options-calculate-class
$pagefile = 'module/class/option.calculate.class.php';
if (is_file($pagefile))
	include_once($pagefile);
$options = new optionsCalculate();

$page->assign('templateTitel', TEMPLATE_TITEL);
$page->assign('Metagenerator', META_GENERATOR);
$page->assign('scripts', xplugin::getHeaderScript());

// if cart called
if (isset($_GET['page']) and isset($_GET['title']) and isset($_GET['name'])
	and isset($_GET['cart']))
{ // Shoppingcart
	$_GET['cart'] = $validate->cleanslash($_GET['cart']);
	$_GET['page'] = $validate->cleanslash($_GET['page']);
	$_GET['title'] = $validate->cleanslash($_GET['title']);
	$_GET['name'] = $validate->cleanslash($_GET['name']);

	if (isset($_POST['number']) and $_GET['cart'] === 'add')
	{ // Add product to cart
		$_POST['number'] = $validate->testNumber($_POST['number'], '');

		$optionText = $options->getOptionsPostText($_GET['title']);
		if ($_GET['cart'] === 'add' and $_GET['page'] === 'article' and $_POST['number'] >= 1)
			$cart->add($_GET['title'], $optionText, $_POST['number']);
	}
	else if (isset($_POST[($_GET['title'] . 'number')]) and $_GET['cart'] === 'newnumber')
	{ // Update shopping cart with new quantity
		$cart->add($_POST['id'], $_GET['name'], $_POST[($_GET['title'] . 'number')], true);
		$_GET['page'] = 'cart';
		$_GET['title'] = 'show';
		$_GET['name'] = _('Shoppingcart');
	}
	else if (isset($_POST['id']) and $_GET['cart'] === 'delete')
	{ // Delete single product
		$cart->delete($_POST['id']);

		if ($cart->isProduct() == 0)
		{
			$_GET['page'] = 'category';
			$_GET['title'] = '0';
			$_GET['name'] = _('Startpage');
		}
		else
		{
			$_GET['page'] = 'cart';
			$_GET['title'] = 'show';
			$_GET['name'] = _('Shoppingcart');
		}
	}
	else if ($_GET['cart'] === 'confirm')
	{ // Call the order-form
		$_GET['page'] = 'cart';
		$_GET['title'] = 'confirm';
		$_GET['name'] = _('Order');
	}
	else if ($_GET['cart'] === 'store')
	{ // Call the order-form
		message::setWarningRow(_('Unfortunately, during the payment process, an error has occurred.'),
			TRUE);
		$_GET['page'] = 'cart';
		$_GET['title'] = 'confirm';
		$_GET['name'] = _('Order');
	}
}
else if (isset($_GET['page']) and isset($_GET['title']) and isset($_GET['name']))
{
	$_GET['page'] = $validate->cleanslash($_GET['page']);
	$_GET['title'] = $validate->cleanslash($_GET['title']);
	$_GET['name'] = $validate->cleanslash($_GET['name']);

	// Testing if forbidden page-parameter
	if ($_GET['page'] <> 'page' and $_GET['page'] <> 'category' and $_GET['page'] <> 'article'
		and ( $_GET['page'] <> 'cart' and $_GET['title'] <> 'show'))
	{
		$_GET['page'] = 'category';
		$_GET['title'] = '0';
		$_GET['name'] = _('Startpage');
	}

	if ($_GET['page'] === 'page')
	{
		$cols = Array('page_active', 'page_header');
		db::getInstance()->where('page_name', $_GET['title']);
		$result = db::getInstance()->getOne("page", null, $cols);

		if (db::getInstance()->count >= 1)
		{
			if ($result['page_active'] === '1')
			{
				$_GET['name'] = $result['page_header'];
			}
			else
			{
				$_GET['title'] = 'startpage';
				$_GET['name'] = 'startpage';
			}
		}
		else
		{
			$_GET['title'] = '0';
			$_GET['name'] = _('Startpage');
		}
	}
	elseif ($_GET['page'] === 'category')
	{
		$cols = Array('category_name');
		db::getInstance()->where('category_id', $_GET['title']);
		$result = db::getInstance()->getOne("category", null, $cols);

		if (db::getInstance()->count >= 1)
		{
			$_GET['name'] = $result['category_name'];
		}
		else
		{
			$_GET['title'] = '0';
			$_GET['name'] = _('Startpage');
		}
	}
	elseif ($_GET['page'] === 'article')
	{
		$cols = Array('product_name');
		db::getInstance()->where('product_id', $_GET['title']);
		$result = db::getInstance()->getOne("product", null, $cols);

		if (db::getInstance()->count >= 1)
		{
			$_GET['name'] = $result['product_name'];
		}
		else
		{
			$_GET['title'] = '0';
			$_GET['name'] = _('Startpage');
		}
	}
}
else if (isset($_GET['page']) and isset($_GET['name']) and USER_LOGIN)
{ // Registrypage
	$_GET['page'] = $validate->cleanslash($_GET['page']);

	if ($_GET['page'] == 'register' and $_GET['name'] == 'newregister')
	{
		$_GET['page'] = 'register';
		$_GET['title'] = '0';
		$_GET['name'] = 'newregister';
	}
	else if ($_GET['page'] == 'register' and $_GET['name'] == 'register')
	{
		$_GET['page'] = 'register';
		$_GET['title'] = '0';
		$_GET['name'] = 'register';
	}
	else if ($_GET['page'] == 'register' and $_GET['name'] == 'captcha')
	{
		$_GET['page'] = 'register';
		$_GET['title'] = '0';
		$_GET['name'] = 'captcha';
	}
}
else
{
	$_GET['page'] = 'category';
	$_GET['title'] = '0';
	$_GET['name'] = _('Startpage');
}

// Settings for template-vars if they not used in every page
$page->assign('contentHead1', '');
$page->assign('contentHead2', '');
$page->assign('contentBottom1', '');
$page->assign('contentBottom2', '');
$page->assign('contentFooter1', '');
$page->assign('contentFooter2', '');
$page->assign('textTop', '');
$page->assign('categorys', '');
$page->assign('textBottom', '');
$page->assign('Metadescription', META_DESCRIPTION);
$page->assign('Metakeywords', META_KEYWORDS);

if ($_GET['name'] == _('Startpage'))
{
	$page->assign('Metakeywords', META_KEYWORDS);
	$page->assign('Metadescription', META_DESCRIPTION);
}
// Staticpage-menu
$pagefile = 'module/menu/staticpage.menu.php';
if (is_file($pagefile))
	include_once($pagefile);
topmenuEntry($page, $_GET['title']);

// Categorypage-menu - overwrite static-content if active
$pagefile = 'module/menu/categorypage.menu.php';
if (is_file($pagefile))
	include_once($pagefile);
$pagefile = 'module/function/article.func.php';
if (is_file($pagefile))
	include_once($pagefile);

categoryMenu($page, $_GET['page'], $_GET['title'], $validate);

// Get minicart
$page->assign('productNumber', $cart->isProduct());
$page->assign('cartLink', '/index.php?page=cart&title=show&name=' . _('Shoppingcart'));

// Get content
$pagefile = 'module/' . $_GET['page'] . '/' . $_GET['page'] . '.php';
if (is_file($pagefile))
	include_once $pagefile;

contentPage($page, $_GET['page'], $_GET['title'], $_GET['name'], $validate, $cart);

if (message::ifErrorMessage())
{
	$page->assign('errorText', message::getErrorText());
	$page->addtpl('errorText', 'errorbox.tpl.php');
}

if (message::ifWarningMessage())
{
	$page->assign('warningText', message::getWarningText());
	$page->addtpl('warningText', 'warningbox.tpl.php');
}

if (message::ifNoteMessage())
{
	$page->assign('noteText', message::getNoteText());
	$page->addtpl('noteText', 'notebox.tpl.php');
}
?>